/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
 
/**
 * @Author: 
 * @apiNote 正则表达式工具类
 */
public class RegexUtil {
 
    private RegexUtil(){}
 
    /**
     * 手机号验证规则
     */
    private static String REGEX_PHONE = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
    /**
     * 整数验证规则
     */
    private static String REGEX_NUMBER = "^[0-9]*[1-9][0-9]*$";
    /**
     * 邮箱验证规则
     */
    private static String REGEX_EMAIL = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$\n";
 
    /**
     * 校验手机号格式是否正确
     *
     * @param phone 手机号
     * @return  true/false
     */
    public static boolean regexPhone(String phone) {
        if ((phone != null) && (!phone.isEmpty())) {
            return Pattern.matches(REGEX_PHONE, phone);
        }
        return false;
    }
 
    /**
     * 校验数据是否是整数
     * @param number 数据
     * @return true/false
     */
    public static boolean regexNumber(String number) {
        if ((number != null) && (!number.isEmpty())) {
            return Pattern.matches(REGEX_NUMBER, number);
        }
        return false;
    }
 
    /**
     * 校验邮箱格式
     *
     * @param email 邮箱
     * @return true/false
     */
    public static boolean isValidEmail(String email) {
        if ((email != null) && (!email.isEmpty())) {
            return Pattern.matches(REGEX_EMAIL, email);
        }
        return false;
    }
 
    /**
     * 校验身份证有效性
     *
     * @param idCard 身份证
     * @return true/false
     */
    public static boolean idCardVerify(String idCard) {
        // 仅适用于18位标准身份证号码
        if (idCard.length() != 18) {
            return false;
        }
        // 使身份证号码中的字母升大写
        idCard = idCard.toUpperCase();
        char[] idCardChars = idCard.toCharArray();
        //加权因子计算
        final int[] factors = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        int sumFactor = 0;
        for (int i = 0; i < factors.length; i++) {
            // 数字的ASCII码是48-57
            int value = idCardChars[i] - 48;
            // 纯数字校验
            if (value < 0 || value > 9) {
                return false;
            }
            sumFactor += factors[i] * value;
        }
        // 校验码比对
        final char[] verifyCode = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        return idCardChars[17] == verifyCode[sumFactor % 11];
    }
 
    /**
     * 判断字符串是否为日期格式(合法)
     *
     * @param inDate:字符串时间
     * @return true/false
     */
    public static boolean isValidDate(String inDate) {
        if (inDate == null) {
            return false;
        }
        // 或yyyy-MM-dd
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMdd");
        if (inDate.trim().length() != dataFormat.toPattern().length()) {
            return false;
        }
        // 该方法用于设置Calendar严格解析字符串;默认为true,宽松解析
        dataFormat.setLenient(false);
        try {
            dataFormat.parse(inDate.trim());
        } catch (ParseException e) {
            return false;
        }
        return true;
    }
 
}